#! /bin/bash

# Hive组件管理
function hive() {
    hiveInfo=$(egrep "hive" $PLAY_HOME/frames.txt)
    hiveNode=$(echo $hiveInfo | cut -d " " -f3)
    nodes=(${hiveNode//,/ })

    case $1 in
    "start") {
        for i in ${nodes[*]}; do
            nohup ssh $i "source /etc/profile && hive --service hiveserver2 & 2>&1 >/dev/null" &
            nohup ssh $i "source /etc/profile && hive --service metastore & 2>&1 >/dev/null" &
        done
    } ;;
    "stop") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && jps | grep RunJar | awk '{print \$1}' | xargs kill -9 "
        done
    } ;;
    esac
}

# Presto组件管理
function presto() {

    prestoInfo=$(egrep "presto" $PLAY_HOME/frames.txt)

    presto=$(echo $prestoInfo | cut -d " " -f1)
    isInstall=$(echo $prestoInfo | cut -d " " -f2)
    installNode=$(echo $prestoInfo | cut -d " " -f3)

    nodes=(${installNode//,/ })

    if [[ $isInstall = "true" ]]; then

        case $1 in
        "start") {
            for i in ${nodes[*]}; do
                ssh $i "source /etc/profile && launcher start" &
            done
        } ;;
        "stop") {
            for i in ${nodes[*]}; do
                ssh $i "source /etc/profile && launcher stop" &
            done
        } ;;
        esac

    else
        echo "presto未安装，请完成安装后再启动！"
    fi
}

# Zeppelin组件管理
function zeppelin() {
    zpInfo=$(egrep "zeppelin" $PLAY_HOME/frames.txt)
    zpNode=$(echo $zpInfo | cut -d " " -f3)
    nodes=(${zpNode//,/ })

    case $1 in
    "start") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && zeppelin-daemon.sh start" &
        done
    } ;;
    "stop") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && zeppelin-daemon.sh stop" &
        done
    } ;;
    esac
}

# Kylin组件管理
function kylin() {
    klInfo=$(egrep "kylin" $PLAY_HOME/frames.txt)
    klNode=$(echo $klInfo | cut -d " " -f3)
    nodes=(${klNode//,/ })

    case $1 in
    "start") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && kylin.sh start" &
        done
    } ;;
    "stop") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && kylin.sh stop" &
        done
    } ;;
    esac
}

# ES组件管理
function es() {
    esInfo=$(egrep "elasticsearch" $PLAY_HOME/frames.txt)
    isInstall=$(echo $esInfo | cut -d " " -f2)
    installNode=$(echo $esInfo | cut -d " " -f3)
    nodes=(${installNode//,/ })

    case $1 in
    "start") {
        for i in ${nodes[*]}; do
            # 使用es用户启动elasticsearch后台执行
            echo "正在启动 $i 节点"
            ssh $i "su -c \"source /etc/profile && elasticsearch -d\" -s /bin/sh es"
        done
    } ;;
    "stop") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && jps | grep Elasticsearch | awk '{print \$1}' | xargs kill -9 "
        done
    } ;;
    esac
}

# DS组件管理
function ds() {
    dsInfo=$(egrep "dolphinscheduler" $PLAY_HOME/frames.txt)
    isInstall=$(echo $dsInfo | cut -d " " -f2)
    installNode=$(echo $dsInfo | cut -d " " -f3)
    nodes=(${installNode//,/ })

    case $1 in
    "start") {
        for i in ${nodes[*]}; do
            echo "正在启动 $i 节点"
            ssh $i "source /etc/profile && bash \$DS_HOME/bin/dolphinscheduler-daemon.sh start standalone-server"
        done
    } ;;
    "stop") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && bash \$DS_HOME/bin/dolphinscheduler-daemon.sh stop standalone-server"
        done
    } ;;
    esac
}

# Zookeeper组件管理
function zookeeper() {

    #1.在frames.txt中查看是否需要安装zk
    zkInfo=$(egrep "^zookeeper" $PLAY_HOME/frames.txt)

    zk=$(echo $zkInfo | cut -d " " -f1)
    isInstall=$(echo $zkInfo | cut -d " " -f2)
    installNode=$(echo $zkInfo | cut -d " " -f3)
    nodes=(${installNode//,/ })

    case $1 in
    "start") {
        for i in ${nodes[*]}; do

            nohup ssh $i "source /etc/profile && zkServer.sh start"
        done
    } ;;
    "stop") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && zkServer.sh stop "
        done
    } ;;
    esac
}

# doris组件管理
function doris_fe() {
    dorisInfo=$(egrep "doris" $PLAY_HOME/frames.txt)
    isInstall=$(echo $dorisInfo | cut -d " " -f2)
    installNode=$(echo $dorisInfo | cut -d " " -f3)
    masterNode=$(echo $dorisInfo | cut -d " " -f4)
    nodes=(${installNode//,/ })

    case $1 in
    "start") {
        for i in "${nodes[@]}"; do
            # 检查当前节点是否是masterNode
            if [ "$i" == "$masterNode" ]; then
                echo "正在启动 $i 作为master节点"
                ssh $i "source /etc/profile && start_fe.sh --daemon"
            else
                echo "正在启动 $i 作为非master节点"
                ssh $i "source /etc/profile && start_fe.sh --helper $masterNode:9010 --daemon"
            fi
        done
    } ;;
    "stop")
        {
            for i in "${nodes[@]}"; do
                ssh $i "source /etc/profile && stop_fe.sh"
            done
        }
        ;;
    esac
}

# doris组件管理
function doris_be() {
    dorisInfo=$(egrep "doris" $PLAY_HOME/frames.txt)
    isInstall=$(echo $dorisInfo | cut -d " " -f2)
    installNode=$(echo $dorisInfo | cut -d " " -f3)
    masterNode=$(echo $dorisInfo | cut -d " " -f4)
    nodes=(${installNode//,/ })

    case $1 in
    "start") {
        for i in ${nodes[*]}; do
            # 使用启动doris后台执行
            echo "正在启动 $i 节点"
            ssh $i "source /etc/profile && sysctl -w vm.max_map_count=2000000 && ulimit -n 60000 && swapoff -a && start_be.sh --daemon"
        done
    } ;;
    "stop") {
        for i in ${nodes[*]}; do
            ssh $i "source /etc/profile && stop_be.sh"
        done
    } ;;
    esac
}

# doris配置
function doris_conf() {
    # 读取Doris安装信息
    dorisInfo=$(egrep "doris" $PLAY_HOME/frames.txt)
    isInstall=$(echo $dorisInfo | cut -d " " -f2)
    installNode=$(echo $dorisInfo | cut -d " " -f3)
    masterNode=$(echo $dorisInfo | cut -d " " -f4)
    nodes=(${installNode//,/ })

    # 读取MySQL安装信息
    mysqlInfo=$(egrep "^mysql-rpm-pack" $PLAY_HOME/frames.txt)
    mysql=$(echo $mysqlInfo | cut -d " " -f1)
    mysqlIsInstall=$(echo $mysqlInfo | cut -d " " -f2)
    mysqlNode=$(echo $mysqlInfo | cut -d " " -f3)

    # 检查MySQL是否已安装
    if [ "$mysqlIsInstall" = "true" ]; then
        # 循环添加FE，如果是masterNode则跳过
        for nodeName in "${nodes[@]}"; do
            if [ "$nodeName" = "$masterNode" ]; then
                echo "Skipping FE addition for masterNode $nodeName"
                continue
            fi
            # 从/etc/hosts文件中获取节点的IP地址
            nodeIP=$(grep -w $nodeName /etc/hosts | awk '{print $1}')
            if [ -z "$nodeIP" ]; then
                echo "Unable to find IP address for $nodeName in /etc/hosts"
                continue # 如果没有找到IP，跳过当前循环
            fi
            echo "Adding FE $nodeName ($nodeIP)"
            ssh $mysqlNode "source /etc/profile && mysql -uroot -h$masterNode -P9030 -e \"ALTER SYSTEM ADD FOLLOWER '\"$nodeIP:9010\"';\""
        done

        # 循环添加BE
        for nodeName in "${nodes[@]}"; do
            # 从/etc/hosts文件中获取节点的IP地址
            nodeIP=$(grep -w $nodeName /etc/hosts | awk '{print $1}')
            if [ -z "$nodeIP" ]; then
                echo "Unable to find IP address for $nodeName in /etc/hosts"
                continue # 如果没有找到IP，跳过当前循环
            fi
            echo "Adding BE $nodeName ($nodeIP)"
            ssh $mysqlNode "source /etc/profile && mysql -uroot -h$masterNode -P9030 -e \"ALTER SYSTEM ADD BACKEND '\"$nodeIP:9050\"';\""
        done
    else
        echo "MySQL is not installed. Please install MySQL first."
    fi
}

if [ "$#" -lt 2 ]; then
    echo "缺少参数，请输入需要执行的脚本和操作命令。"
    echo "示例：./脚本名.sh [脚本] [操作命令]"
    echo "eg：pl_manager hive start"
    exit 1
fi

# Hive
if [ $1 == "hive" ]; then

    case $2 in
    start)
        echo "执行Hive启动命令..."
        hive start
        echo "Hive已启动"
        ;;

    stop)
        echo "执行Hive停止命令..."
        hive stop
        echo "Hive已停止"
        ;;

    *)
        echo "无效的Hive操作命令。可用命令：start, stop"
        ;;

    esac
fi

# Presto
if [ $1 == "presto" ]; then

    case $2 in
    start)
        echo "执行Presto启动命令..."
        presto start
        ;;

    stop)
        echo "执行Presto停止命令..."
        presto stop
        ;;

    *)
        echo "无效的Presto操作命令。可用命令：start, stop"
        ;;

    esac
fi

# Zeppelin
if [ $1 == "zeppelin" ]; then

    case $2 in
    start)
        echo "执行Zeppelin启动命令..."
        zeppelin start
        ;;

    stop)
        echo "执行Zeppelin停止命令..."
        zeppelin stop
        ;;

    *)
        echo "无效的Zeppelin操作命令。可用命令：start, stop"
        ;;
    esac
fi

# Kylin
if [ $1 == "kylin" ]; then

    case $2 in
    start)
        echo "执行Kylin启动命令..."
        kylin start
        ;;

    stop)
        echo "执行Kylin停止命令..."
        kylin stop
        ;;

    *)
        echo "无效的Kylin操作命令。可用命令：start, stop"
        ;;
    esac
fi

# es
if [ $1 == "es" ]; then

    case $2 in
    start)
        echo "执行ES启动命令..."
        es start
        ;;

    stop)
        echo "执行ES停止命令..."
        es stop
        ;;

    *)
        echo "无效的ES操作命令。可用命令：start, stop"
        ;;
    esac
fi

# ds
if [ $1 == "ds" ]; then

    case $2 in
    start)
        echo "执行DS启动命令..."
        ds start
        ;;

    stop)
        echo "执行DS停止命令..."
        ds stop
        ;;

    *)
        echo "无效的DS操作命令。可用命令：start, stop"
        ;;
    esac
fi

# zk
if [ $1 == "zookeeper" ]; then

    case $2 in
    start)
        echo "执行ZK启动命令..."
        zookeeper start
        ;;

    stop)
        echo "执行ZK停止命令..."
        zookeeper stop
        ;;

    *)
        echo "无效的ZK操作命令。可用命令：start, stop"
        ;;
    esac
fi

if [ $1 == "doris" ]; then
    if [ $2 == "fe" ]; then
        if [ $3 == "start" ]; then
            doris_fe start
        elif [ $3 == "stop" ]; then
            doris_fe stop
        else
            echo "Invalid command for FE. Use 'start' or 'stop'."
        fi
    elif [ $2 == "be" ]; then
        if [ $3 == "start" ]; then
            doris_be "start"
        elif [ $3 == "stop" ]; then
            doris_be "stop"
        else
            echo "Invalid command for BE. Use 'start' or 'stop'."
        fi
    elif [ $2 == "add" ]; then
        doris_conf
    else
        echo "Invalid component. Use 'fe' or 'be'."
    fi
fi
